class TasksController < ApplicationController
   before_filter :find_task,  :only => [:show, :edit, :update, :destroy] 
  
  layout "gestion"

  access_control do
    allow :team_leader
    allow :chief_of_staff
  end

  # GET /tasks
  def index
    @tasks = Task.all
  end

  # GET /tasks/1
  def show
  end

  # GET /tasks/new
  def new
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  def create
    @task = Task.new(params[:task])

      if @task.save
        redirect_to(@task)
      else
      	redirect_to(new_task_path)
      end
  end

  # PUT /tasks/1
  def update
	if @task.update_attributes(params[:task])
      redirect_to(@task) 
    else
      redirect_to(edit_task_path)
    end
  end

  # DELETE /tasks/1
  def destroy
  	@taskProject = ProjectTask.find(:all, :conditions => { :task_id => params[:id] }) 
    @task.destroy
       
   	@taskProject.each() do |task|
		task.destroy    	
    end
    redirect_to(tasks_url)
  end
  
  private 
	def find_task 
		@task = Task.find(params[:id])  
	end 
end
